package com.mmp.main.servlet;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mmp.common.bo.Message;
import com.mmp.common.bo.Message.MESSAGE_TYPE;
import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.HospitalDevice;
import com.mmp.main.util.Default;
import com.mmp.main.util.Enums;
import com.mmp.main.util.WebUtil;

@WebServlet(urlPatterns = { "/UpdateDeviceInfo", "/TransferDevice", "/ScrappDevice", "/PpmFollowup", "/OpenJobOrder",
		"/ViewDeviceCard", "/ViewDeviceDetailsReport" })
public class SearchForDeviceProcessServlet extends BasicServlet {

	private static HashMap<String, String> destinations;

	static {
		destinations = new HashMap<String, String>();
		destinations.put("UpdateDeviceInfo", "DeviceInfoDisplay");
		destinations.put("TransferDevice", "DeviceTransferringDisplay");
		destinations.put("ScrappDevice", "DeviceScrappingDisplay");
		destinations.put("PpmFollowup", "PPMVisitDisplay");
		destinations.put("OpenJobOrder", "JobOrderOpenDisplay");
		destinations.put("ViewDeviceCard", "ViewDeviceProfile");
		destinations.put("ViewDeviceDetailsReport", "ViewDeviceDetails");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String dest = getSearchDestination(destinations, request);
		String code = WebUtil.getParamValue(request, "deviceCode", null);
		String sn = WebUtil.getParamValue(request, "deviceSerial", null);
		int siteId = getSessionSiteId(request);
		int locationId = getSessionLocationId(request);

		HospitalDevice searchResult = dbQueryManager.getDevice(siteId, locationId, code, sn, null, false, false);

		String msg = null;

		if (searchResult == null) {
			msg = "لم يتم العثور علي أي نتائج موافقة لبحثك";
		} else {

			// message prefix
			String msgPrefix = "عفوا .. لا يمكن إتمام عملية البحث .. ";

			if ("JobOrderOpenDisplay".equals(dest)) {
				if (searchResult.getStatus() == Enums.DEVICE_STATUS.UNDER_MAINTENANCE.getStatus())
					msg = "يوجد أمر عمل مفتوح للجهاز المطلوب";
				else if (searchResult.getStatus() == Enums.DEVICE_STATUS.SCRAPPED.getStatus())
					msg = "الجهاز المطلوب تم تكهينه";
				else if (searchResult.getStatus() == Enums.DEVICE_STATUS.TRANSFERRED.getStatus())
					msg = "الجهاز المطلوب تم نقله أو سحبه";
			} else if ("DeviceInfoDisplay".equals(dest) || "PPMVisitDisplay".equals(dest)) {
				if (searchResult.getStatus() == Enums.DEVICE_STATUS.SCRAPPED.getStatus())
					msg = "الجهاز المطلوب تم تكهينه";
				else if (searchResult.getStatus() == Enums.DEVICE_STATUS.TRANSFERRED.getStatus())
					msg = "الجهاز المطلوب تم نقله أو سحبه";
			} else if ("DeviceScrappingDisplay".equals(dest)) {
				if (searchResult.getStatus() == Enums.DEVICE_STATUS.TRANSFERRED.getStatus())
					msg = "الجهاز المطلوب تم نقله أو سحبه";
			} else if ("DeviceTransferringDisplay".equals(dest)) {
				if (searchResult.getStatus() == Enums.DEVICE_STATUS.SCRAPPED.getStatus())
					msg = "الجهاز المطلوب تم تكهينه";
				else if (searchResult.getStatus() == Enums.DEVICE_STATUS.TRANSFERRED.getStatus())
					msg = "الجهاز المطلوب تم نقله أو سحبه بالفعل";
			}

			if (msg != null)
				msg = msgPrefix + msg;

		}

		if (msg == null) { // search success
			request.setAttribute(Default.DEVICE_INFO_ATTR_NAME, searchResult);
			basicForward(request, response, dest);
			return;
		}

		if (WebUtil.getParamValue(request, "report", null) != null) {
			announceErorrMessage(request, response, msg);
		} else {
			setMessage(request, new Message(msg, MESSAGE_TYPE.ERROR));
			basicForward(request, response, WebUtil.getSearchDisplayURL(request));
		}
	}
}
